package router

import (
	"crow-k8s/src/config"
	"crow-k8s/src/service"
	"github.com/gin-gonic/gin"
)

// GetServiceList 查看service列表
// @Summary 查看service列表
// @Description 查看service列表
// @Tags Service
// @Param namespace_name query string true "namespace名字"
// @Success 200 {object} response.Response{data=v1.ServiceList} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/service/list [get]
// @Security Bearer
func GetServiceList(c *gin.Context) {
	namespaceName := c.Query("namespace_name")
	serviceList, err := service.GetServiceList(config.K8sClientSet, namespaceName)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, serviceList)
}

// GetService 查看指定service
// @Summary 查查看指定service
// @Description 查看指定service
// @Tags Service
// @Param namespace_name query string true "namespace名字"
// @Param name query string true "service名字"
// @Success 200 {object} response.Response{data=v1.Service} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/service [get]
// @Security Bearer
func GetService(c *gin.Context) {
	namespaceName := c.Query("namespace_name")
	serviceName := c.Query("name")
	serviceInfo, err := service.GetService(config.K8sClientSet, namespaceName, serviceName)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, serviceInfo)
}

// CreateService 创建service
// @Summary 创建service
// @Description 创建service
// @Tags Service
// @Accept  application/json
// @Product application/json
// @Param data body  service.CreateServiceRequest true "创建service所需的信息"
// @Success 200 {object} response.Response{data=v1.Service} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/service [post]
// @Security Bearer
func CreateService(c *gin.Context) {
	var request service.CreateServiceRequest
	err := c.Bind(&request)
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	serviceInfo, err := service.CreateService(config.K8sClientSet, request)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, serviceInfo)
}

// ApplyService 修改service
// @Summary 修改service
// @Description 修改service
// @Tags Service
// @Accept  application/json
// @Product application/json
// @Param data body  service.ApplyServiceRequest true "修改service所需的信息"
// @Success 200 {object} response.Response{data=v1.Service} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/service [put]
// @Security Bearer
func ApplyService(c *gin.Context) {
	var request service.ApplyServiceRequest
	err := c.Bind(&request)
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	serviceInfo, err := service.ApplyServiceByPort(config.K8sClientSet, request)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, serviceInfo)
}

// DeleteService 删除service
// @Summary 删除service
// @Description 删除service
// @Tags Service
// @Accept  application/json
// @Product application/json
// @Param data body  service.DeleteServiceRequest true "删除service所需的信息"
// @Success 200 {object} response.Response{data=string} "{"requestId": "string","code": 200,"msg": "ok","data": [...]}"
// @Failure 500 {object} response.Response{msg=string} "{"requestId": "string","code": 500,"msg": "string","status": "error","data": null}"
// @Router /api/v1/k8s/service [delete]
// @Security Bearer
func DeleteService(c *gin.Context) {
	var request service.DeleteServiceRequest
	err := c.Bind(&request)
	if err != nil {
		SetErrDefault(c, err)
		return
	}
	err = service.DeleteService(config.K8sClientSet, request)
	for err != nil {
		SetErrDefault(c, err)
		return
	}
	SetOK(c, "删除成功")
}
